package com.gui.components.search;

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import com.utils.search.QuranSearcher;
import com.utils.search.SearchResults;

public class SearchPanel extends JPanel implements ActionListener,KeyListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3609591308056354216L;
	JTextField searchTextField;
	JLabel searchLabel;
	JTextPane ayahTextPane;
	JTextPane searchResultsSummary;
	SearchResultsTable searchResultsTable=new SearchResultsTable();
	
	public SearchPanel()
	{
		searchTextField=new JTextField(30);
		searchLabel=new JLabel("Type To Search");
		ayahTextPane=new JTextPane();
		searchResultsSummary=new JTextPane();
		searchResultsSummary.setEditable(false);
		//add listeners
		searchTextField.addKeyListener(this);
		//setting orientation
		searchTextField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		initGui();
	}

	private void initGui()
	{
		this.setLayout(new BorderLayout());
		this.add(this.getNorthPanel(),BorderLayout.NORTH);
		this.add(new JScrollPane(searchResultsTable),BorderLayout.CENTER);
		
		
	}
	public void actionPerformed(ActionEvent e) {

		
	}
	

	JPanel getNorthPanel()
	{
		JPanel northPanel=new JPanel();
		JPanel topPanel=new JPanel();
		northPanel.setLayout(new BorderLayout());
		northPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		topPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		searchResultsSummary.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		topPanel.add(this.searchLabel);
		topPanel.add(this.searchTextField);
		
		northPanel.add(topPanel,BorderLayout.NORTH);
		northPanel.add(this.searchResultsSummary,BorderLayout.CENTER);
		return northPanel;
	}

	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void keyReleased(KeyEvent e) {
		SearchResults searchResults= QuranSearcher.search(searchTextField.getText());
		searchResultsTable.setSearchResults(searchResults);
		
		String searchSummary="search keyword : '"+searchResults.getSearchPattern()+"'";
		searchSummary+="\nNumber Of Matches = "+searchResults.getMatchedAyat().size()+" Ayah(s)";
		this.searchResultsSummary.setText(searchSummary);
	}

	public void keyTyped(KeyEvent e) {
	}
	
	

}
